package com.atguigu.Serivce.Impl;

import com.atguigu.GlobalExceptionHandler.CustomException;
import com.atguigu.Serivce.CategoryService;
import com.atguigu.Utils.R;
import com.atguigu.mapper.CategoryMapper;
import com.atguigu.mapper.DishMapper;
import com.atguigu.mapper.SetmealMapper;
import com.atguigu.pojo.Category;
import com.atguigu.pojo.Dish;
import com.atguigu.pojo.Setmeal;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
   private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public R page(Integer page, Integer pageSize) {
        Page<Category> pageInfo =new Page<>(page,pageSize);
        LambdaQueryWrapper<Category>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        queryWrapper.orderByAsc(Category::getUpdateTime);
        categoryMapper.selectPage(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    @Override
    public R add(Category category) {
//        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(Category::getName,category.getName());
//
//
//        Category category1 = categoryMapper.selectOne(queryWrapper);
//        if ( category1!= null) {
//            return R.error("该菜品/套餐已存在");
//        }
//        category.setCreateTime(new Date());
//        category.setUpdateTime(new Date());
//        category.setUpdateUser((Long) attribute);
//        category.setCreateUser((Long) attribute);

        categoryMapper.insert(category);
        return R.success("添加成功");
    }

    @Override
    public R updateCategory(Category category) {
        //建议使用全局异常处理器，这样对于菜品名或套餐名不修改的修改就不会阻止
            categoryMapper.updateById(category);
            return R.success("修改成功");

    }

    @Override
    public R deleteCategory(Long ids) {
        //这里是关联删除，要先判断表是否有关联关系
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper =new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,ids);

        //查询要删除的是否关联了菜品
        Long l = dishMapper.selectCount(dishLambdaQueryWrapper);
        if (l>0) {
            //关联了菜品，抛出一个异常
            throw new CustomException("关联某些菜品,无法删除");
        }


        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,ids);

        //查询要删除的是否关联了套餐
        Long l1 = setmealMapper.selectCount(setmealLambdaQueryWrapper);
        if (l1>0) {
            //关联了套餐，抛出一个异常
            throw new CustomException("关联某些套餐,无法删除");
        }


        //没有关联关系，正常删除
        categoryMapper.deleteById(ids);
        return R.success("删除成功");
    }

    @Override
    public R listType(Integer type) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != type,Category::getType,type);
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> categories = categoryMapper.selectList(queryWrapper);
        return R.success(categories);
    }


}
